package com.cloudptt.api.product.func.sys;

import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.product.func.IBroadcast;
import com.cloudptt.api.product.func.sys.fsm.SysAgent;
import com.cloudptt.api.product.func.sys.fsm.SysConst;
import com.cloudptt.api.product.func.sys.listener.SysMsgListener;
import com.cloudptt.api.product.log.Log;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class SysBroadcast extends BroadcastReceiver implements IBroadcast
{
	public final static String TimerOutKey_Clear = "clearData";
	private Context ctx = null;
	private SysAgent sysAgent = null;
//	private AlarmTimer keepLiveTimer = null;
	private SysMsgListener sysMsgListener = null;
	private volatile int keepAliveTimeoutCount = 0;

	public SysBroadcast(Context ctx,SysAgent sysAgent )
	{
		Log.d(SysFunc.Log_Key, "SysBroadcast");
		this.ctx = ctx;
		this.sysAgent = sysAgent;

		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_STACK_CHANGED));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_REG));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_FOREDETACH));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_SVR_RESTART));

		//		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_NETWORK_CHANGED));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_MESSAGE_SYS));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SIP_KEEPALIVE));
		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SYS_NOTIFY));
//		this.ctx.registerReceiver(this, new IntentFilter(SipManager.ACTION_SYS_PUBLISH));

//		this.ctx.registerReceiver( this, new IntentFilter(Intent.ACTION_LOCALE_CHANGED) );
//		if(Product.getProductType() != ProductConst.ProductType_NAPP_RADIO)
//		{
//			this.startTimer();
//		}
	}

	protected void setSysMsgListener(SysMsgListener sysMsgListener) {
		this.sysMsgListener = sysMsgListener;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.d(SysFunc.Log_Key, "Action is " + action);

		if (action.equals(SipManager.ACTION_SIP_STACK_CHANGED)) 
		{
			int svrState = intent.getIntExtra(SipManager.SIPSTACK_STATUS,-1);
			Log.d(SysFunc.Log_Key, "ACTION_SIP_STACK_CHANGED status is " + svrState);
			if(svrState == -1)
			{
				return;
			}
			if(SipManager.SIPSTACK_START == svrState)
			{
				sysAgent.recevSysMsg(SysConst.SysMsg_Svr_Start);
			}
			else if(SipManager.SIPSTACK_STOP == svrState)
			{
				sysAgent.recevSysMsg(SysConst.SysMsg_Svr_Stop);
			}
		}
		else if(action.equals(SipManager.ACTION_SIP_REG))
		{
			int code = intent.getIntExtra(SipManager.RESULT_CODE,0);//.getStringExtra(SipManager.RESULT_NAME);//intent.getFlags();
			String num = intent.getStringExtra(SipManager.RESULT_NUM);
			String name = intent.getStringExtra(SipManager.RESULT_NAME);
			Log.d(SysFunc.Log_Key, "ACTION_SIP_REG code = " + code + " name = " + name);
			sysAgent.onSipAuthResult(code,name,num);
		}
		else if( action.equals(SipManager.ACTION_SIP_KEEPALIVE) )
		{
			keepAliveTimeoutCount = 0;
			if(sysAgent != null)
			{
				sysAgent.doKeepAlive(true);
			}
		}
		else if( action.equals(SipManager.ACTION_SIP_FOREDETACH) )
		{
			Log.d(SysFunc.Log_Key, "ACTION_SIP_FOREDETACH");
			if(sysAgent != null)
			{
				sysAgent.doForceDetach();
			}
		}
		else if( action.equals(SipManager.ACTION_SIP_SVR_RESTART) )
		{
			Log.d(SysFunc.Log_Key, "ACTION_SIP_SVR_RESTART");
			if(sysAgent != null)
			{
				sysAgent.doSvrRestart();
			}
		}
		else if( action.equals( SipManager.ACTION_SYS_NOTIFY ) )
		{
//			String xmlStr = intent.getStringExtra(SipManager.XML_STR);
//			if( this.configCenter != null
//					&& config != null
//					&& xmlStr != null
//					&& xmlStr.length() > 0)
//			{
//				String cmsUrl = config.getServerInfo().toCmsHttpUrl();
//				String gmsUrl = config.getServerInfo().toGmsHttpUrl();
//				configCenter.recvNotify(cmsUrl,gmsUrl,xmlStr, this);
//			}
		}
		else if(intent.getAction().equals(SipManager.ACTION_SIP_MESSAGE_SYS)) //鎺ュ彈鐭秷鎭箍鎾�
		{
		}
	}

//	public void startTimer() {
//		Log.d(SysFunc.Log_Key, "startTimer");
//		stopTimer();
//		this.keepLiveTimer = new AlarmTimer(ctx,TimerOutKey_Clear,20,this);
//		keepLiveTimer.startRefreshTimer();
//	}
//	public void stopTimer() {
//		Log.d(SysFunc.Log_Key, "stopTimer");
//		if(keepLiveTimer != null)
//		{
//			this.keepLiveTimer.stopRefreshTimer();
//			this.keepLiveTimer = null;
//		}
//	}
//	
//	@Override
//	public void timeIntervalTimeout(String alarmAction) {
//		// TODO Auto-generated method stub
//		Log.d(SysFunc.Log_Key, "timeIntervalTimeout alarmAction = " + alarmAction);
//		if(TimerOutKey_Clear.equals(alarmAction))
//		{
//			doTimerOutClear();
//		}
//	}
	private void doTimerOutClear()
	{
		Log.d(SysFunc.Log_Key, "doTimerOutClear keepAliveTimeoutCount = " + keepAliveTimeoutCount);
		if(keepAliveTimeoutCount >= 3)
		{
			if(sysAgent != null)
			{
				sysAgent.doKeepAlive(false);
			}
		}
		keepAliveTimeoutCount ++;
	}

	public void clearResource()
	{
		Log.d(SysFunc.Log_Key, "SysBroadcast clearResource");
//		this.sysListener = null;
		this.sysMsgListener = null;
//		this.loginListener = null;
//		this.config.clearUserInfoAfterExit();
//		this.config  = null;
//		this.stopTimer();
		this.ctx.unregisterReceiver(this);
		this.ctx = null;
	}

	public void clearResourceForLogout()
	{
		Log.d(SysFunc.Log_Key, "SysBroadcast clearResourceForLogout");
//		this.config.clearUserInfoAfterLogout();
	}
}


